package client;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import JDBC.JDBCTools;

public class business {
	public static void main(String args[]) throws SQLException {

		System.out.println("你好欢迎使用Qiaoxingyu's Contancts");

		while (true) {
			System.out.println("1、查看联系人");
			System.out.println("2、添加联系人");
			System.out.println("3、修改联系人");
			System.out.println("4、删除联系人");
			System.out.println("5.Quit");
			@SuppressWarnings("resource")
			Scanner input = new Scanner(System.in);
			int op = input.nextInt();

			if (op == 5)
				break;
			else if (op < 5) {
				if (op == 1) {
					System.out.println("输入联系人姓名或电话");

					// String q = input.next();
					String op2 = input.next();

					JDBCTools jdbc = new JDBCTools();

					ResultSet rs1 = jdbc.select_name(op2);
					ResultSet rs2 = jdbc.select_phone(op2);

					System.out.println("系统id		用户名		电话");

					if (rs1 != null) {
						while (rs1.next()) {
							System.out.println(
									rs1.getString(1) + "		" + rs1.getString(2) + "	" + rs1.getString(3));
						}
					}
					if (rs2 != null) {
						while (rs2.next()) {
							System.out.println(
									rs2.getString(1) + "		" + rs2.getString(2) + "		" + rs2.getString(3));
						}
					}
				}
				if (op == 2) {
					System.out.println("输入联系人姓名和电话");

					String name, phone;
					name = input.next();
					phone = input.next();

					JDBCTools jdbc = new JDBCTools();

					if (jdbc.insert(name, phone)) {
						System.out.println("添加成功");
					} else {
						System.out.println("添加失败");
					}
				}
				if (op == 3) {
					System.out.println("输入要修改的系统id");
					int op2 = input.nextInt();

					System.out.println("输入要修改的内容(1、name, 2、phone):");
					int bool = input.nextInt();

					System.out.println("输入修改后的值");
					String op3 = input.next();

					JDBCTools jdbc = new JDBCTools();
					if (bool == 1)
						jdbc.update_name(op2, op3);
					if (bool == 2)
						jdbc.update_phone(op2, op3);
				}
				if (op == 4) {
					System.out.println("输入要删除的系统id");
					int op2 = input.nextInt();

					JDBCTools jdbc = new JDBCTools();

					if (jdbc.delete(op2)) {
						System.out.println("删除成功");
					} else {
						System.out.println("删除失败");
					}

				}
			} else
				continue;
		}
	}
}